{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 相邻不重复\n",
    "\n",
    "[![Author](https://img.shields.io/badge/Author-%40mjd507-important?style=social&logo=GitHub)](https://github.com/mjd507)\n",
    "[![WeChat Channel](https://img.shields.io/badge/Wechat-Daily%20Problem-important?style=social&logo=WeChat)](https://mp.weixin.qq.com/s/sIBCBnerPrdvgggY4KT8GQ)\n",
    "\n",
    "![Logo](img/Day81-1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　给定一个字符串，重新排列使每个字符不存在相邻的相同字符。如果不存在这样的重排列，则返回`None`。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用例说明\n",
    "\n",
    "```python\n",
    "In[1]: abbccc\n",
    "Out[1]: cbcbca\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rearrangeString(s: str) -> str:\n",
    "    queue = [[s.count(char), char] for char in set(s)]\n",
    "    queue.sort(key=lambda x: x[0])\n",
    "    result = ''\n",
    "    prev = [-1, '#']\n",
    "    while queue != []:\n",
    "        char = queue.pop()\n",
    "        result += char[1]\n",
    "        if prev[0] > 0:\n",
    "            queue.append(prev)\n",
    "            queue.sort(key=lambda x: x[0])\n",
    "        char[0] -= 1\n",
    "        prev = char\n",
    "    if len(s) != len(result):\n",
    "        return None\n",
    "    else:\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cbcbca\n"
     ]
    }
   ],
   "source": [
    "print(rearrangeString('abbccc'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
